package com.magicborrow.adapter;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.magicborrow.R;
import com.magicborrow.beans.AddressBean;

public class AddressAdapter extends BaseAdapter {
    private boolean hasIcon;

    public void setHasIcon(boolean hasIcon) {
        this.hasIcon = hasIcon;
    }

    private List<AddressBean> data = new ArrayList<AddressBean>();

    private Context context;
    private LayoutInflater layoutInflater;

    public AddressAdapter(Context context, ArrayList<AddressBean> data) {
        this.context = context;
        this.layoutInflater = LayoutInflater.from(context);
        this.data = data;
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public AddressBean getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = layoutInflater.inflate(R.layout.address_item_layout, null);
            convertView.setTag(new ViewHolder(convertView));
        }
        ViewHolder holder = (ViewHolder) convertView.getTag();
        if (hasIcon) {
            if (position == 0) {
                holder.imvIcon.setImageDrawable(convertView.getContext().getResources().getDrawable(R.mipmap.icon_marker));
            } else if (position == 1) {
                holder.imvIcon.setImageDrawable(convertView.getContext().getResources().getDrawable(R.mipmap.icon_location));

            } else {
                holder.imvIcon.setImageDrawable(new BitmapDrawable());
            }
        } else {
            holder.imvIcon.setVisibility(View.GONE);
        }
        if (position >= getCount() - 1)
            holder.v.setVisibility(View.GONE);
        else
            holder.v.setVisibility(View.VISIBLE);
        initializeViews(getItem(position), holder);
        return convertView;
    }

    private void initializeViews(AddressBean object, ViewHolder holder) {
        //TODO implement
        holder.tvName.setText(object.getAddress());
        holder.tvAddress.setText(object.getAddressInfo());
    }

    protected class ViewHolder {
        private ImageView imvIcon;
        private TextView tvName;
        private TextView tvAddress;
        private View v;

        public ViewHolder(View view) {
            imvIcon = (ImageView) view.findViewById(R.id.imv_icon);
            tvName = (TextView) view.findViewById(R.id.tv_name);
            tvAddress = (TextView) view.findViewById(R.id.tv_address);
            v = view.findViewById(R.id.v);
        }
    }
}
